<?php
/**
 * 广告管理
 * @author cjli
 *
 */
class AdvAction extends HomeAction
{
	//adloader JS调用广告位
	public function ad_position_info()
	{
		$id = intval($_GET['id']);
		$data=array('flag'=>false,'data'=>'');
		if($id==0)
		{
			$data['data']=L('_PARAM_ERROR_');
			echo json_encode($data);
			die();
		}
		$re = AdvModel::get_Position_info($id);
		if(!$re)
		{
			echo json_encode($data);
			die();
		}
		$data['data'] = $re;
		$data['flag'] = true;
		echo json_encode($data);
		die();
	}
	//adloader JS调用广告列表
	public function ad_list()
	{
		$data = array('flag'=>false,'data'=>'');
		$id = intval($_GET['id']);
		if($id==0)
		{
			die( json_encode($data)  );
		}
		$id = intval($id);

		$advList = AdvModel::get_adv_list_by_position_id($id);
		if(!$advList)
		{
			echo json_encode($data);
			die();
		}
		$data['flag']=true;
		$data['data']=$advList;
		echo json_encode($data );
		die();
	}
	
	/**
	 * 直接代码调用
	 * @example <script src="/adv/getadimg/position_id/6"></script>
	 * @return string
	 */
  public function getadimg ()
  {
  	$id =  isset($_REQUEST['id']) ? intval($_REQUEST['id']) : 0; //广告标识或者广告ID号
  	$position_id = intval($_REQUEST['position_id']); //广告位ID号
  	$width = isset($_REQUEST['width']) ? intval($_REQUEST['width']) : null; //广告宽度
  	$height = isset($_REQUEST['height']) ? intval($_REQUEST['height']) : null; //广告高度
  	if($position_id){
  		$advList = AdvModel::get_adv_list_by_position_id($position_id);
  		//$advInfo = $advList ? reset($advList) : false;
  		if($advList){
	  		foreach ($advList as $adv){
	  			if (strpos($adv['link'], 'http') !== false) {
	  				echo 'document.write(\'<a href="'.$adv['link'].'" target="_blank"><img src="'. $adv['content'] .'"'. ($adv['width']? ' width="'.$adv['width'].'"' : null) . ($adv['height']? ' height="'.$adv['height'].'"' : null).' alt="'.$adv['description'].'"></a>\');';
	  		  } else {
	  		  	echo 'document.write(\'<img src="'. $adv['content'] .'"'.  ($adv['width']? ' width="'.$adv['width'].'"' : null) . ($adv['height']? ' height="'.$adv['height'].'"' : null).' alt="'+$adv['description']+'">\');';
	  		  }
	  		}
  		}
  	}elseif ($id) {
  		$advInfo = AdvModel::getAdvInfo($id);
  	
  	if (isset($advInfo) && $advInfo) {
  		$advInfo = (object)$advInfo;
  		$advInfo->width = $width ? $width : ($advInfo->width != 0 ? $advInfo->width : null);
  		$advInfo->height = $height ? $height : ($advInfo->height != 0 ? $advInfo->height : null);
  		if ($advInfo->ad_type == 1) {
  			$html = 'document.write(\'';
  			if (strpos($advInfo->link, 'http') !== false) {
  				$html .='<a href="'.$advInfo->link.'" target="_blank">';
  			}
  				$html .='<img src="'. $advInfo->content .'"';
  				$html .= $advInfo->width ? ' width="'. $advInfo->width.'"' : null;
  				$html .= $advInfo->height ? ' height="'. $advInfo->height.'"' : null;
  				$html .= $advInfo->description ? ' alt="'. $advInfo->description.'"' : null;
  				$html .= ' />';
  			if (strpos($advInfo->link, 'http') !== false) {
  				$html .='</a>';
  			}
  			$html .='\');';
  			echo $html;
  		} elseif ($advInfo->ad_type == 2 ) {
  			$str = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="'. $advInfo->width .'" height="'. $advInfo->height .'">';
        $str .= '<param name="movie" value="/images/'.$advInfo->filepath.'" >';
        $str .= '<param name="quality" value="high" >';
        $str .= '<embed src="/images/'.$advInfo->content.'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'. $advInfo->width .'" height="'. $advInfo->height .'"></embed></object>';
        echo "document.write('$str')";
  		} elseif ($advInfo->ad_type == 3 ) {
  			if (strpos($advInfo->link, 'http') !== false) {
  				echo 'document.write(\'<a href="'.$advInfo->link.'" target="_blank">'.trim($advInfo->content).'</a>\');';
  		  } else {
  		  	echo 'document.write(\''.trim($advInfo->content).'\');';
  		  }
      } elseif ($advInfo->ad_type == 4 ) {
  		  echo 'document.write(\''.trim($advInfo->content).'\');';
      }
  	}
	  }else{
	  	return false;
	  }
  }
}